#include <bits/stdc++.h>
using namespace std;
#include <atcoder/lazysegtree>
using namespace atcoder;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'

struct S {
  ll zero, one, inversion;
};
S op(S l, S r) {
  return S{l.zero + r.zero, l.one + r.one,
           l.inversion + r.inversion + l.one * r.zero};
}
S e() { return S{0, 0, 0}; }

using F = bool;
S mapping(F l, S r) {
  if (!l) return r;
  return S{r.one, r.zero, r.one * r.zero - r.inversion};
}
F composition(F l, F r) { return l != r; }
F id() { return false; }

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<S> a(n);
  rep(i, 0, n - 1) {
    int x;
    cin >> x;
    if (x == 0)
      a[i] = S{1, 0, 0};
    else
      a[i] = S{0, 1, 0};
  }
  lazy_segtree<S, op, e, F, mapping, composition, id> seg(a);
  while (m--) {
    int op, l, r;
    cin >> op >> l >> r;
    l--;
    if (op == 1)
      seg.apply(l, r, true);
    else
      cout << seg.prod(l, r).inversion << endl;
  }
  return 0;
}